package com.company;

import java.util.ArrayList;
import java.util.List;

public class Test31约瑟夫环 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for(int i = 0;i<10;i++){
            list.add(i);
        }
        System.out.println(yue(list,4,2));
    }

    /**
     * 约瑟夫环
     * @param list
     * @param begin
     * @param interval
     * @return
     */
    public  static int yue(List<Integer> list,int begin,int interval){
        int size = list.size();
        if(size<=1){
            return list.get(0);
        }else{
            begin = begin + interval-1;
            if(begin>=size){
                begin=begin-size;
            }
            list.remove(begin);
            return yue(list,begin,interval);
        }
    }
}
